package com.macro.mall.portal.controller;

import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.model.TiTrainingSignRecordLog;
import com.macro.mall.model.UmsMember;
import com.macro.mall.portal.service.TiTrainingSignService;
import com.macro.mall.portal.service.UmsMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.security.Principal;

/**
 * TiTrainingSign的路由接口服务
 *
 * @author
 */
@Slf4j
@RestController
@RequestMapping("TiTrainingSignController")
@Api(tags = "签到签退信息表", description = "签到签退信息表")
public class TiTrainingSignController {

    /**
     * TiTrainingSignService服务
     */
    @Autowired
    private TiTrainingSignService tiTrainingSignService;
    @Autowired
    private UmsMemberService memberService;

    /**
     * 查询所有数据的方法
     * 统计
     * @param
     * @return
     */
    @GetMapping(value = "/list", produces = {"application/json;charset=UTF-8"})
    public CommonResult find(@RequestParam(value = "courseName",required = false) String courseName,
                             @RequestParam(value = "communityName",required = false) String communityName,
                             @RequestParam(value = "phone",required = false) String phone,
                             @RequestParam(value = "userIdentity",required = false) String userIdentity,
                             @RequestParam(value = "signStatus",required = false) String signStatus,
                             @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                             @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        return CommonResult.success(CommonPage.restPage(tiTrainingSignService.find(courseName,communityName,phone,userIdentity,signStatus,pageNum,pageSize)));
    }

    /**
     * 通过id查询数据的方法
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/getById", produces = {"application/json;charset=UTF-8"})
    public CommonResult findOne(@RequestParam(name = "id") Integer id) {
        return CommonResult.success(tiTrainingSignService.findOne(id));
    }
    /**
     * 课程签到签退配置接口
     * @param request
     * @return
     */
    @ApiOperation("课程签到签退配置接口")
    @RequestMapping(value = "/signConfig", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult signConfig(@ApiParam(value = "请求报文") @RequestBody TiTrainingSignRecordLog request,Principal principal) {
        if(principal==null){
            return CommonResult.unauthorized(null);
        }
        UmsMember member = memberService.getCurrentMember();
        return tiTrainingSignService.signConfig(request,member);
    }

    /**
     * 查询更新属性不为空的数据方法
     * 更新FinishWatchTime
     * @param
     * @return
     */
    @PostMapping(value = "/update", produces = {"application/json;charset=UTF-8"})
    public CommonResult update(@Valid @RequestBody TiTrainingSignRecordLog value,Principal principal) {
        if(principal==null){
            return CommonResult.unauthorized(null);
        }
        UmsMember member = memberService.getCurrentMember();
        CommonResult data = tiTrainingSignService.updateNotNull(value,member);
        return CommonResult.success(null);
    }

    /**
     * 查询当前用户video进度
     *
     * @param
     * @return
     */
    @PostMapping(value = "/select", produces = {"application/json;charset=UTF-8"})
    public CommonResult select(@Valid @RequestBody TiTrainingSignRecordLog value,Principal principal) {
        if(principal==null){
            return CommonResult.unauthorized(null);
        }
        UmsMember member = memberService.getCurrentMember();
        return tiTrainingSignService.selectOne(value,member);
    }
}
